# -*- coding:utf-8 -*-
__author__ = 'XF'

'Tools'

import hashlib
import random
import time


def encrypt_md5(text=None):

    if text:
        md5 = hashlib.md5()
        md5.update(text.encode('utf-8'))
        text_md5 = md5.hexdigest()
    else:
        text_md5 = None

    return text_md5


# 编号生成
def id_generate(how_many=1, only=True, length=6, prefix='', suffix=''):
    """

    :param how_many: 生成编号的条数
    :param only: 生成的编号是否唯一
    :param length: 长度
    :param prefix: 前缀
    :param suffix: 后缀
    :return:
    """
    if only:
        gen_length = length - len(prefix) - len(suffix)
        id_list = set()
        while len(id_list) < how_many:
            randomStr = str(time.time()).split('.')
            left_length = random.randint(1, gen_length-1)
            right_length = gen_length - left_length

            pos1 = random.randint(0, len(randomStr[0])-1)
            id_left = ''
            while left_length > 0:
                id_left += randomStr[0][pos1 % len(randomStr[0])]
                pos1 += 1
                left_length -= 1

            pos2 = random.randint(0, len(randomStr[1])-1)
            id_right = ''
            while right_length > 0:
                id_right += randomStr[1][pos2 % len(randomStr[1])]
                pos2 += 1
                right_length -= 1

            id_list.add(prefix + id_left + id_right + suffix)

    return list(id_list)


if __name__ == '__main__':
    pass